/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.layouts.BoxLayout;
import core.IMAPMIDlet;
import core.ScreenEngine;
import view.additional.CellItem;
import view.additional.MenuList;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class ConnectionListForm extends BasicForm implements MenuList.MenuListSelection
{
    private static final int OK_COMMAND_ID = 1;
    
    private static final int CALL_DISP_ITEM_ID = 0;
    private static final int SEND_MESSAGE_ITEM_ID = 1;
    private static final int DISP_LIFE_ITEM_ID = 2;
    private static final int DISP_MTS_ITEM_ID = 3;
    private static final int DISP_KIEVSTAR_ITEM_ID = 4;
    
    private MenuList items;
    private CellItem callDispItem;
    private CellItem sendMessageItem;
    private CellItem dispLifeItem;
    private CellItem dispMTSItem;
    private CellItem dispKievstarItem;
    
    private SendMessageForm sendMessageForm;
    
    public ConnectionListForm()
    {
        super("connection");
        
        init();
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setScrollable(false);
        
        items = new MenuList();
        callDispItem = new CellItem(CALL_DISP_ITEM_ID, "disp_call");
        //Пока не отображаем
        //items.addItem(callDispItem);
        sendMessageItem = new CellItem(SEND_MESSAGE_ITEM_ID, "send_mess");
        items.addItem(sendMessageItem);
        dispLifeItem = new CellItem(DISP_LIFE_ITEM_ID, "disp_life");
        items.addItem(dispLifeItem);
        dispMTSItem = new CellItem(DISP_MTS_ITEM_ID, "disp_mts");
        items.addItem(dispMTSItem);
        dispKievstarItem = new CellItem(DISP_KIEVSTAR_ITEM_ID, "disp_kievstar");
        items.addItem(dispKievstarItem);        
        items.setSelectionListener(this);
        addComponent(items);
        
        //Команды
        addCommand(new BackCommand());
        
        Command ok = new Command("ok", OK_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                itemSelected(items.getSelectedId());
                
                evt.consume();
            }
        };
        addCommand(ok);
    }
    
    public void show()
    {
        //Устанавливаем активные\неактивные пункты
        setItemsState();
        
        super.show();
    }
    
    public void itemSelected(int id)
    {
        switch(id)
        {
            //ToDo Kvest
            //case CALL_DISP_ITEM_ID :  break;
            case SEND_MESSAGE_ITEM_ID : showSendMessageForm(); break;
            case DISP_LIFE_ITEM_ID : doCall(IMAPMIDlet.getMidlet().getDispLifeNumber()); break;
            case DISP_MTS_ITEM_ID : doCall(IMAPMIDlet.getMidlet().getDispMTSNumber()); break;
            case DISP_KIEVSTAR_ITEM_ID : doCall(IMAPMIDlet.getMidlet().getDispKievstarNumber()); break;
        }
    }
    
    private void setItemsState()
    {
        callDispItem.setEnabled(state == CONNECTED_STATE);
        sendMessageItem.setEnabled(state == CONNECTED_STATE);
        dispLifeItem.setEnabled(state == CONNECTED_STATE);
        dispMTSItem.setEnabled(state == CONNECTED_STATE);
        dispKievstarItem.setEnabled(state == CONNECTED_STATE);
    }
    
    private void showSendMessageForm()
    {
        if (sendMessageForm == null)
        {
            sendMessageForm = new SendMessageForm();
        }
        
        ScreenEngine.getEngine().push(sendMessageForm);
    }
    
    private void doCall(String number)
    {
        if (!number.equals(""))
        {        
            //Звоним
            String url = "tel:" + number;
            try
            {
                IMAPMIDlet.getMidlet().platformRequest(url);
            }
            catch(Exception e){}
        }

        //Возвращаемся к главному меню
        ScreenEngine.getEngine().popTo(0);
    }
}
